$(function() {
$("#searchid").linkbutton({
	 text:'查询',
	 iconCls:'icon-search'
});


	$('#brandlist').datagrid({
		title :'您所在的位置：拼购管理模块&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;商家管理',
		idField : 'id',
		nowrap : false,
		striped : true,
		collapsible : false,
		url : '../brand/search.shtml?t='+(new Date().getTime().toString(36)),
		width : 1000,
		height : 600,
		frozenColumns : [[{
					field : 'ck',
					title : '选择',
					checkbox : true
				}]],
		columns : [[{
					title : '基本信息',
					colspan : 4
				}, {
					field : 'opt',
					title : '操作',
					width : 270,
					align : 'center',
					rowspan : 2,
					formatter : function(value, rec) {
						if (rec.id == undefined)
							return null;
						else
							return '<span style="color:red"><span onclick=Onmodify("'
									+ rec.id
									+ '")><font style="cursor:pointer">修改</span></font> <span onclick=Ondelete("'
									+ rec.id + '")><font style="cursor:pointer">删除</font>&nbsp;</span><a href="javascript:changeActivity(\''+rec.activityFlag+'\',\''+rec.id+'\')">活动设置</a></span><hr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select id="aud_'+rec.sysuserId +'"><option value="1">审核通过</option><option value="2">审核不通过</option></select>&nbsp;<span onclick=OnAutding("'
									+rec.sysuserId+'")><font style="cursor:pointer">审核</font></span></span>';
					}
				}], [{
					field : 'name',
					title : '品牌',
					align:'center',
					width : 230
				},{
					field : 'imgsrc',
					title : '商标',
					align:'center',
					width : 180,
					formatter : function(value, rec) {
						return '<img src="../'+value+'" width="144px" height="45px"/>';
					}
				},	
				{field:'activityFlag',title:'活动状态',width:120,align:'center',formatter:function(value){if(value=="1") return "是";else return "否"}},
				{
					field : 'auding',
					title : '审核状态',
					align:'center',
					width : 100,
					formatter : function(value, rec) {
						if(value==1)return "审核通过";
					    else if(value==2)return "审核未通过";
						else  return  "<font color='red'>未审核</font>";
					}
				}

		]],
		pagination : true,
		singleSelect:true,
		pageList:[10,20,50,80,100],
		rownumbers : true,
		queryParams : {
			name : name,
			auding : ''
		},
		toolbar : [{
					id : 'btnadd',
					text : '新增商家',
					iconCls : 'icon-add',
					handler : function() {
						$(this).attr("href", '../brand/findMessageById.shtml?id=0');
					}
				}, '-',{
					id : 'btnadd',
					text : '信息维护',
					iconCls : 'icon-add',
					handler : function() {
					    var records = $('#brandlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要维护的记录');
							return;
						}
						if(records.length>1){
							alert('只能逐条维护');
							return;
						}
						$(this).attr("href", '../shoperMessage/findshopMessageBybrandId.shtml?brandid='+records[0].id+'&name='+$("#name").val()+'&auding='+$("#auding").val());
					}
				}, '-',/*{
					id : 'btnsave',
					text : '删除商家',
					iconCls : 'icon-remove',
					handler : function() {
						var records = $('#brandlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要删除的记录');
							return;
						}
						if (confirm("确定删除此条信息吗?")) {
						for (var i = 0; i < records.length; i++) {
							if (records[i].id == undefined
									|| records[i].id == "undefined") {

								alert('此条记录已不存在');
								return;
							}
							ids += records[i].id;
							ids += '_';
						}
					    deleteById(ids);
					

					}
					}
				}, '-',*/{
					id : 'btnadd',
					text : '行业类别',
					iconCls : 'icon-add',
					handler : function() {
						var records = $('#brandlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要维护的记录');
							return;
						}
						if(records.length>1){
							alert('只能逐条维护');
							return;
						}
						$(this).attr("href", 'brand_type.jsp?menuCls=33&id='+records[0].id+'&name='+$("#name").val()+'&auding='+$("#auding").val());
					}
				}, '-',{
					id : 'btnadd',
					text : '品牌公告维护',
					iconCls : 'icon-add',
					handler : function() {
						var records = $('#brandlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要维护的记录');
							return;
						}
						if(records.length>1){
							alert('只能逐条维护');
							return;
						}
						$(this).attr("href", '../brand/findMessageById.shtml?id='+records[0].id+'&flag=1'+'&name='+$("#name").val()+'&auding='+$("#auding").val());
					}
				}, '-',{
					id : 'btnadd',
					text : '店铺信息维护',
					iconCls : 'icon-add',
					handler : function() {
						var records = $('#brandlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要维护的记录');
							return;
						}
						if(records.length>1){
							alert('只能逐条维护');
							return;
						}
						$(this).attr("href", '../brand/findStoreInfo.shtml?id='+records[0].id+'&name='+$("#name").val()+'&auding='+$("#auding").val());
					}
				},'-',{
					id : 'btnadd',
					text : '活动信息维护',
					iconCls : 'icon-add',
					handler : function() {
						var records = $('#brandlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要维护的记录');
							return;
						}
						if(records.length>1){
							alert('只能逐条维护');
							return;
						}
						$(this).attr("href", '../brand/manageFavorable.jsp?id='+records[0].id+'&name='+$("#name").val()+'&auding='+$("#auding").val());
					}
				},'-',{
					id : 'btnadd',
					text : '客户案例维护',
					iconCls : 'icon-add',
					handler : function() {
						var records = $('#brandlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要维护的记录');
							return;
						}
						if(records.length>1){
							alert('只能逐条维护');
							return;
						}
						$(this).attr("href", '../brand/manageCase.jsp?id='+records[0].id+'&name='+$("#name").val()+'&auding='+$("#auding").val());
					}
				},'-',{
					id : 'btnadd',
					text : '产品维护',
					iconCls : 'icon-add',
					handler : function() {
						var records = $('#brandlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要维护的记录');
							return;
						}
						if(records.length>1){
							alert('只能逐条维护');
							return;
						}
						$(this).attr("href", '../brand/productList.jsp?id='+records[0].id+'&name='+$("#name").val()+'&auding='+$("#auding").val());
					}
				},'-',{
					id : 'marketbtn',
					text : '所属市场维护',
					iconCls : 'icon-add',
					handler : function() {
						var records = $('#brandlist').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要维护的记录');
							return;
						}
						if(records.length>1){
							alert('只能逐条维护');
							return;
						}
						$(this).attr("href", '../shoper/brand_market.jsp?id='+records[0].id+'&name='+$("#name").val()+'&auding='+$("#auding").val());
					}
				}, '-',{
					id : 'btnclose',
					text : '  &nbsp;关  闭',
					iconCls : 'icon-cancel',
					handler : function() {
						window.self.close();
					}
				}
				
				]
	}

	);
     $(".searchcenter").keydown(function(){
	     if(event.keyCode==13)
         {
           onSearch();
         }
	    });
	    
	$("#modifyWindow").dialog({
		title:'活动商家设置',
		width:300,
		height:120,
		resizable:false,
		minimizable:false,
		maximizable:false,
		toolbar: [{
			text : '保存设置',
		    iconCls:'icon-save',
		    handler:function(){
						var url1 = "../brand/changeActivity.shtml";
						var tt = document.getElementsByName('activityflag');
						var activityflag = 0;
						for(var i = 0;i<tt.length;i++){   
			                if(tt[i].checked)   
			                   activityflag = tt[i].value;   
			             }  
						var infoId = $("#infoId").val();
						$.ajax({
							url : url1,
							data : "infoId=" + infoId+"&activityflag="+activityflag,
							success : function(msg) {
								if(msg=="1"){
									alert("保存成功");							
								}else{
									alert("保存失败");
								}
									$("#modifyWindow").dialog('close');
								$('#brandlist').datagrid("reload");
								
							}
						});
		    }
		  }],
		closed:true
	})		    
	    
	    
	    
});
function deleteById(id){
   $.ajax({
		url : "../brand/deleteById.shtml?ids=" + id,
		success : function(msg) {
			var obj = eval('(' + msg + ')');
			var result = obj["result"];
			if (result == "3") {
				alert('该品牌被设置了拼购或者广告，禁止删除');
			} else {
				if (!result) {
					alert("删除成功失败");
					return;
				}
				alert("删除成功");
				$('#brandlist').datagrid("reload");
			}
		}
	});
}



   function   onSearch(){
            var queryParams =$('#brandlist').datagrid("options").queryParams;
			queryParams.name=$("#name").val();
			queryParams.auding=$("#auding").val();
			queryParams.activityFlag=$("#activityFlag").val();
			$('#brandlist').datagrid("reload");
   }


function Ondelete(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定删除此条信息吗?")) {

		deleteById(id);
	}
}
function Onmodify(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	window.location.href = "../brand/findMessageById.shtml?id=" + id ;
}
function OnAutding(sysuserId) {
	if (sysuserId == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定审核此条信息吗?")) {
        var autding=document.getElementById("aud_"+sysuserId).value;
		var url1 = "../brand/updateMessageById.shtml";
						$.ajax({
									url : url1,
									data : "autding="+autding+"&sysuserId="+sysuserId ,
									cache:false,
									success : function(msg) {

										var obj = eval('(' + msg + ')');
				                       var result = obj["result"];
			                          if(!result){
                                            alert("审核失败");
                                           return;
						                     }
						                  alert("审核成功");
										$('#brandlist').datagrid("reload");
									}
								});
	}
}
function changeActivity(activityFlag,infoId) {
     var tt = document.getElementsByName('activityflag');
        for (var iIndex = 0; iIndex < tt.length ; iIndex++ )
        {   
           if(tt[iIndex].value==activityFlag){
            tt[iIndex].checked=true;
            break;
           }
        } 
	$("#infoId").val(infoId);
	$("#modifyWindow").window('open');
}
